package com.jwaoo.chat.viewholder;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.method.LinkMovementMethod;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;
import com.dx.as.common.sys.ScreenUtil;
import com.jwaoo.chat.ChatConstants;
import com.jwaoo.chat.ChatHelpter;
import com.jwaoo.chat.R;
import com.jwaoo.chat.session.emoji.MoonUtil;
import com.jwaoo.chat.ui.recyclerview.adpter.BaseMultiItemFetchLoadAdapter;

/**
 * Created by zhoujianghua on 2015/8/4.
 */
public class MsgViewHolderText extends MsgViewHolderBase {

    public MsgViewHolderText(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
        return R.layout.nim_message_item_text;
    }

    @Override
    protected void inflateContentView() {
    }

    @Override
    protected void bindContentView() {
        layoutDirection();
        TextView bodyTextView = findViewById(R.id.nim_message_item_text_body);
        bodyTextView.setTextColor(isReceivedMessage() ? Color.BLACK : Color.WHITE);

        //富媒体处理
        MoonUtil.identifyFaceExpression(ChatHelpter.getContext(), bodyTextView, getDisplayText(), ImageSpan.ALIGN_BOTTOM);
//        bodyTextView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void layoutDirection() {
        TextView bodyTextView = findViewById(R.id.nim_message_item_text_body);
        if (isReceivedMessage()) {
            bodyTextView.setBackgroundResource(R.drawable.pop_left_bg);
            if (message.getSnap() == ChatConstants.snap){
                setDrawable(bodyTextView,R.drawable.fire_read_gray_small);
            }
            bodyTextView.setPadding(ScreenUtil.dip2px(20), ScreenUtil.dip2px(8), ScreenUtil.dip2px(15), ScreenUtil.dip2px(8));
        } else {
            if (message.getSnap() == ChatConstants.snap) {

                if (message.getIsRead() == ChatConstants.unRead){
                    bodyTextView.setBackgroundResource(R.drawable.fire_pop_right_bg);

                }else {
                    setDrawable(bodyTextView,R.drawable.fire_read_pink_small);
                    bodyTextView.setBackgroundResource(R.drawable.fire_pop_right_bg_read);
                }

            }else {
                bodyTextView.setBackgroundResource(R.drawable.pop_right_bg);
            }
            bodyTextView.setPadding(ScreenUtil.dip2px(15), ScreenUtil.dip2px(8), ScreenUtil.dip2px(20), ScreenUtil.dip2px(8));
        }
    }

    private void setDrawable(TextView v ,int drawableId){
        if (Build.VERSION.SDK_INT >= 21){
            Drawable drawable = context.getDrawable(drawableId);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());
            v.setCompoundDrawablePadding(10);
            v.setCompoundDrawables(drawable,null,null,null);
        }
    }

    @Override
    protected void onItemClick() {
        super.onItemClick();
        message.setIsRead(ChatConstants.isRead);
        bindContentView();
    }

    @Override
    protected int leftBackground() {
        return 0;
    }

    @Override
    protected int rightBackground() {
        return 0;
    }

    protected String getDisplayText() {
        if (message.getSnap() == ChatConstants.snap && message.getIsRead() == ChatConstants.unRead){
            return "点击查看!";
        }
        return message.getContent();
    }
}
